package java8.myStream;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能描述:按测试分组
 * 2024/09/09
 *
 * @author ZhangYi
 */
public class GroupByTest {

	public static void main(String[] args) {
		List<Person> peoples = Arrays.asList(
				new Person("Alice", 20),
				new Person("Bob", 17),
				new Person("Charlie", 22),
				new Person("Diana", 19),
				new Person("Eve", 25),
				new Person("Frank", 16)
		);

		Map<Integer, List<Person>> peopleAgeMap = peoples.stream().collect(Collectors.groupingBy(Person::getAge));
		System.out.println(peopleAgeMap);

		Map<Integer, Map<String, List<Person>>> map = peoples.stream().collect(Collectors.groupingBy(Person::getAge, Collectors.groupingBy(Person::getName)));
		System.out.println(map);

		Map<Object, List<Person>> collect = peoples.stream().collect(Collectors.groupingBy((person) -> {
			if (person.getAge() < 3){
				return "3";
			}else if (person.getAge() < 18){
				return "18";
			}else if (person.getAge() < 25){
				return "25";
			}
			return "null";
		}));

		System.out.println(collect);

		Map<Boolean, List<Person>> collect1 = peoples.stream().collect(Collectors.partitioningBy(person -> person.getAge() <19 ));
		System.out.println(collect1);
	}
}
